<?php 

function sendMsg($phone,$msg){
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/send.json");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-cc801eb0ce1819ab5966c22a2e1b10b1');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array("mobile"=>$phone,"message"=>$msg."【极速牌照】"));
        $res = curl_exec( $ch );
        curl_close( $ch );
        
        $response = json_decode($res);
        if($response->msg == 'ok'){
            //session(array('code'=>$code,'expire'=>3600));
            cookie("code",$code);
        }
        return $response->msg == 'ok';
    }

function checkorderstatus($ordid){
    $Ord=M('order');
    $ordstatus=$Ord->where('ordcode='.$ordid)->getField('status');
    if($ordstatus==3){
        $msg="订单号:".$Ord['id']." 金额:".$Ord['total_price'];
        sendMsg("18511013273",$msg);
        return true;

    }else{
        return false;    
    }
 }
  
 //处理订单函数
 //更新订单状态，写入订单支付后返回的数据
 function orderhandle($parameter){
    $ordid=$parameter['out_trade_no'];
    
    $data=M('order')->where(array("ordcode"=>$ordid))->find();
    $data['payment_trade_no']      =$parameter['trade_no'];
    $data['payment_trade_status']  =$parameter['trade_status'];
    $data['payment_notify_id']     =$parameter['notify_id'];
    $data['payment_notify_time']   =$parameter['notify_time'];
    $data['payment_buyer_email']   =$parameter['buyer_email'];
    $data['update_at']=date("Y-m-d h:i:s");
    $data['status'] =3;
    M('order')->where('ordcode='.$ordid)->save($data);
 } 
  
  
  
 /*-----------------------------------
2013.8.13更正
下面这个函数，其实不需要，大家可以把他删掉，
具体看我下面的修正补充部分的说明
------------------------------------*/
  
 //获取一个随机且唯一的订单号；
 function getordcode(){
    $Ord=M('order');
    $numbers = range (10,99);
    shuffle ($numbers); 
    $code=array_slice($numbers,0,4); 
    $ordcode=$code[0].$code[1].$code[2].$code[3];
    $oldcode=$Ord->where("ordcode='".$ordcode."'")->getField('ordcode');
    if($oldcode){
        getordcode();
    }else{
        return $ordcode;
    }
 }


